package ysq.xyg.shop.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.extend.ehcache.EhCacheService;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.shop.dao.ShopBannerDao;
import ysq.xyg.shop.model.po.ShopBanner;
import ysq.xyg.sys.model.po.SysUser;
import ysq.xyg.shop.model.vo.ShopBannerVo;
import ysq.xyg.shop.service.ShopBannerService;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 商城模块-轮播图service实现
 *
 * @author congcong tao
 * @date 2019-4-28 9:06:41
 */
@Service("shopBannerService")
public class ShopBannerServiceImpl extends BaseServiceImpl<ShopBanner> implements ShopBannerService {

    @Autowired
    private EhCacheService ehCacheService;
    @Resource
    private ShopBannerDao shopBannerDao;

    private ShopBanner shopBanner;

    @Autowired
    public void setBaseDao(ShopBannerDao shopBannerDao) {
        this.baseDao = shopBannerDao;
    }


    /**
     * 分页查询
     *
     * @param shopBannerVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-4-28 9:06:41
     */
    @Override
    public ReturnEntity page(ShopBannerVo shopBannerVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" update_time desc");
        List<ShopBanner> list = acceptList(shopBannerVo);
        PageInfo<ShopBanner> page=new PageInfo<ShopBanner>(list);
        return new ReturnEntity(page.getList(),page.getTotal(),page.isHasNextPage());
    }

    /**
     * 修改
     *
     * @param shopBannerVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-4-28 9:06:41
     */
    @Override
    public ReturnEntity addOrUpdate(ShopBannerVo shopBannerVo, SysUser currentUser) {
        boolean isUpate=true;
        if(StrUtil.isEmpty(shopBannerVo.getId())){
            shopBanner=new ShopBanner(currentUser);
            isUpate=false;
        }else{
            shopBanner=selectByPrimaryKey(shopBannerVo.getId());
            if(ObjectUtil.isNull(shopBanner)){
                return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
            }
            shopBanner.setUpdateTime(new Date());
            shopBanner.setUpdateUser(currentUser.getId());
        }
        shopBanner.setType(shopBannerVo.getType());
        shopBanner.setTitle(shopBannerVo.getTitle());
        shopBanner.setImage(shopBannerVo.getImage());
        shopBanner.setOrderNum(shopBannerVo.getOrderNum());
        shopBanner.setStatus(shopBannerVo.getStatus());
        shopBanner.setUrl(shopBannerVo.getUrl());
        int num=0;
        if(isUpate){
            num=updateByPrimaryKey(shopBanner);
        }else{
            num=insert(shopBanner);
        }
        if(num<=0){
            return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
        }
        ehCacheService.evictShopBanner();
        return new ReturnEntity();
    }

    /**
     * 删除
     *
     * @param id 主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-4-28 9:06:41
     */
    @Override
    public ReturnEntity delete(String id, SysUser currentUser) {
        ShopBanner shopBanner=selectByPrimaryKey(id);
        if(shopBanner!=null){
            shopBanner.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            shopBanner.setUpdateTime(new Date());
            shopBanner.setUpdateUser(currentUser.getId());
            int num=updateByPrimaryKey(shopBanner);
            if(num>0){
                ehCacheService.evictShopBanner();
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
    }

    /**
     * 获取集合
     *
     * @param shopBannerVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author congcong tao
     * @date 2019-4-28 9:06:41
     */
    @Override
    public List<ShopBanner> acceptList(ShopBannerVo shopBannerVo){
        Example example = new Example(ShopBanner.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotEmpty(shopBannerVo.getRemoveTag())){
            criteria.andEqualTo("removeTag",shopBannerVo.getRemoveTag());
        }
        example.setOrderByClause(" update_time desc");
        return shopBannerDao.selectByExample(example);
    }
}
